import http from "@/api";
import qs from "qs";

/**
 * @name 资产管理模块
 */

// 资产列表查询
export const cItemsFind = (txt, params) => {
  return http.post(`/pages/ci!cItemsFind.action?${txt}`, qs.stringify(params), { loading: false });
};
//资产统计
export const ciItemsCount = params => {
  return http.post(`/pages/ci!ciFlotPieData.action`, qs.stringify(params), { loading: false });
};
// 资产分类
export const getConfigurationCategoryTree = params => {
  return http.post(`/pages/ciCategory!getConfigurationCategoryTree.action`, qs.stringify(params), { loading: false });
};

// 资产详情
export const findById = ciId => {
  return http.post(`/pages/ci!findById.action?ciEditId=${ciId}`, qs.stringify({}), { loading: false });
};

// 资产删除
export const cItemsDel = (txt, params) => {
  return http.post(`/pages/ci!cItemsDel.action?${txt}`, qs.stringify({}), { loading: false });
};

// 附件列表
export const findAllAttachment = params => {
  return http.post(`/pages/ci!findAllAttachment.action`, qs.stringify(params), { loading: false });
};

// 关联配置项列表
export const ciRelevanceList = (ciId, params) => {
  return http.post(`/pages/itsm/cim/ciRelevance!find.action?ciRelevanceId=${ciId}`, qs.stringify(params), { loading: false });
};

// 关联请求
export const findRequests = (txt, params) => {
  return http.post(`/pages/request!findRequests.action?${txt}`, qs.stringify(params), { loading: false });
};

// 关联变更
export const findPagerChange = (txt, params) => {
  return http.post(`/pages/change!findPagerChange.action?${txt}`, qs.stringify(params), { loading: false });
};

// 关联问题
export const findProblemPager = (txt, params) => {
  return http.post(`/pages/problem!findProblemPager.action?${txt}`, qs.stringify(params), { loading: false });
};

// 历史更新
export const findCiHistoryUpdate = params => {
  return http.post(`/pages/ci!findCiHistoryUpdate.action`, qs.stringify(params), { loading: false });
};

// 新增资产
export const cItemSave = (txt, params, other) => {
  Object.keys(other).forEach(key => {
    if (`${key}`.includes("eav_")) {
      params["ciDto.attrVals['" + `${key}` + "']"] = `${other[key]}`;
    }
  });
  return http.post(`/pages/ci!cItemSave.action?${txt}`, qs.stringify(params), { loading: false });
};

// 资产编辑
export const cItemUpdate = (txt, params, other) => {
  Object.keys(other).forEach(key => {
    if (`${key}`.includes("eav_")) {
      params["ciDto.attrVals['" + `${key}` + "']"] = `${other[key]}`;
    }
  });
  return http.post(`/pages/ci!cItemUpdate.action?${txt}`, qs.stringify(params), { loading: false });
};

// 资产导出
export const exportConfigureItem = params => {
  return http.post(`/pages/ci!exportConfigureItem.action`, qs.stringify(params), { loading: false });
};

// 资产模板下载
export const createTemplateWithCategory = categoryNo => {
  return http.downloaditsm(`/pages/importCIAction!createTemplateWithCategory.action?categoryNo=${categoryNo}`);
};

// 导入资产模板数据
export const importConfigureItem = (text, params) => {
  return http.post(`/pages/ci!importConfigureItem.action?${text}`, qs.stringify(params), { loading: false });
};

// 查看修改行为
export const findBehaviorModificationByid = params => {
  return http.post(`/pages/ci!findBehaviorModificationByid.action`, qs.stringify(params), { loading: false });
};

// 显示/下载二维码
export const findImage = ciId => {
  return http.downloaditsm(`/pages/ciScanAction!findImage.action?ciScanDTO.ciId=${ciId}`, qs.stringify({}), { loading: false });
};

// 生成二维码
export const saveScan = params => {
  return http.post(`/pages/ciScanAction!SaveScan.action`, qs.stringify(params), { loading: false });
};

// 资产管理导出文件列表查询
export const findExportInfoPager = (companyNo, params) => {
  return http.post(`/pages/exportInfo!findExportInfoPager.action?exportInfoDTO.companyNo=${companyNo}`, qs.stringify(params), {
    loading: false
  });
};

// 资产管理按文件名导出文件
export const exportData = params => {
  return http.downloaditsm(`/pages/ci!exportData.action`, qs.stringify(params), { loading: false });
};

// 删除附件
export const deleteAttachement = (ciEditId, aid) => {
  return http.post(`/pages/ci!deleteAttachement.action?ciEditId=${ciEditId}&aid=${aid}`, qs.stringify({}), { loading: false });
};

//查询自定义模板
export const findAllTemplate = params => {
  return http.post(
    "/pages/template!findAllTemplate.action?templateDTO.templateType=configureItem&templateDTO.categoryNo=" + params,
    qs.stringify(params)
  );
};

// 通过登录名查询资产
export const findPageConfigureItemByUser = (params: {}, header: string, unhandle: string[]) => {
  let newobj = {};
  if (header != null && header != "") {
    Object.keys(params).forEach(key => {
      if (unhandle.length > 0) {
        if (unhandle.includes(`${key}`)) {
          newobj[`${key}`] = `${params[key]}`;
        } else {
          newobj[header + "." + `${key}`] = `${params[key]}`;
        }
      } else {
        newobj[header + "." + `${key}`] = `${params[key]}`;
      }
    });
  } else {
    newobj = params;
  }
  return http.post(`/pages/ci!findPageConfigureItemByUser.action`, qs.stringify(newobj));
};
